﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace TimeTracker
{
    partial class AboutBox : Form
    {
        const double lengthofHourHand = 60;

        public AboutBox()
        {
            InitializeComponent();
            this.Text = String.Format("About {0}", AssemblyTitle);
            this.labelProductName.Text = AssemblyProduct;
            this.labelVersion.Text = String.Format("Version {0}", AssemblyVersion);
            this.labelCopyright.Text = AssemblyCopyright;
            this.labelCompanyName.Text = AssemblyCompany;
            this.textBoxDescription.Text = AssemblyDescription;
        }

        #region Assembly Attribute Accessors

        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion

        private void timer_Animation_Tick(object sender, EventArgs e)
        {
            using (Graphics display = logoPictureBox.CreateGraphics())
            {
                using (Image i = new Bitmap(logoPictureBox.Image))
                {
                    using (Graphics g = Graphics.FromImage(i))
                    {
                        float lengthofHourHand = (float)this.Height / 3 / 1.65F;
                        float lengthofMinuteHand = (float)this.Height / 3 / 1.40F;
                        float lengthofSecondHand = (float)this.Height / 3 / 1.35F;

                        double hourRadian = (DateTime.Now.Hour % 12 + DateTime.Now.Minute / 60F) * 30 * Math.PI / 180;
                        double minRadian = (DateTime.Now.Minute + DateTime.Now.Second / 60F) * 6 * Math.PI / 180;
                        double secondRadian = (DateTime.Now.Second + (double)DateTime.Now.Millisecond / 1000) * 6 * Math.PI / 180;

                        double hourEndPointX = lengthofHourHand * Math.Sin(hourRadian);
                        double hourEndPointY = lengthofHourHand * Math.Cos(hourRadian);

                        double minuteEndPointX = lengthofMinuteHand * Math.Sin(minRadian);
                        double minuteEndPointY = lengthofMinuteHand * Math.Cos(minRadian);

                        double secondEndPointX = lengthofSecondHand * Math.Sin(secondRadian);
                        double secondEndPointY = lengthofSecondHand * Math.Cos(secondRadian);

                        DrawLine(2F, lengthofSecondHand, Color.Gray, (float)secondRadian, g);
                        DrawLine(4F, lengthofMinuteHand, Color.Gray, (float)minRadian, g);
                        DrawLine(4F, lengthofHourHand, Color.Gray, (float)hourRadian, g);

                        display.DrawImage(i, 0 - Math.Abs((i.Width / 2) - (logoPictureBox.Width / 2)), 0 - Math.Abs((i.Height / 2) - (logoPictureBox.Height / 2)));
                    }
                }
            }
        }


        private void DrawLine(float fThickness, float fLength, Color color,
          float fRadians, Graphics g)
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            float fCenterX = logoPictureBox.Image.Width / 2;
            float fCenterY = logoPictureBox.Image.Height / 2;
            using (Pen p = new Pen(color, fThickness))
            {
                g.DrawLine(p,
                fCenterX - (float)(fLength / 9 * System.Math.Sin(fRadians)),
                fCenterY + (float)(fLength / 9 * System.Math.Cos(fRadians)),
                fCenterX + (float)(fLength * System.Math.Sin(fRadians)),
                fCenterY - (float)(fLength * System.Math.Cos(fRadians)));
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
            this.Close();
        }

        private void AboutBox_Load(object sender, EventArgs e)
        {

        }
    }
}
